package com.warmlight.voicepacket.ui.communitylm.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.github.siyamed.shapeimageview.RoundedImageView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.warmlight.voicepacket.R;

import java.util.ArrayList;
import java.util.List;

public class ReplyImgAdapter extends RecyclerView.Adapter<ReplyImgAdapter.Myholer> {

    private Context mContext;
    private ArrayList<LocalMedia> mImgs;
    private RecyclerView recyclerView;
    private PhotoCallBack mCallBack;

    public interface PhotoCallBack{
        void addPhoto();
        void deletePhoto(int position);
        void showImg(boolean show);
    }

    public ReplyImgAdapter(Context context,PhotoCallBack callBack, ArrayList<LocalMedia>imgs){
        this.mContext = context;
        this.mImgs = imgs;
        this.mCallBack = callBack;
    }

    @Override
    public Myholer onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate( R.layout.adapter_reply_img,parent,false);
        Myholer myholer = new Myholer(view);
        return myholer;
    }

    @Override
    public void onBindViewHolder(Myholer holder, final int position) {
        final LocalMedia localMedia = mImgs.get(position);
        final String s = localMedia.getPath();
        if (TextUtils.equals("addImg",s)){
            holder.ri_img.setImageResource(R.drawable.icon_reply_add_img);
            holder.iv_img_delete.setVisibility(View.GONE);
            holder.iv_gif.setVisibility(View.GONE);
        }else{
            Glide.with(mContext).asBitmap().load(s).into(holder.ri_img);
            holder.iv_img_delete.setVisibility(View.VISIBLE);
            if (PictureMimeType.isGif(localMedia.getPictureType())){
                holder.iv_gif.setVisibility(View.VISIBLE);
            }else {
                holder.iv_gif.setVisibility(View.GONE);
            }
        }

        holder.ri_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.equals("addImg",s)){
                    if (mCallBack != null){
                        mCallBack.addPhoto();
                    }
                }else{
                    ArrayList<LocalMedia> m = new ArrayList<>();
                    m.addAll(mImgs);
                    if (TextUtils.equals("addImg",m.get(mImgs.size()-1).getPath())){
                        m.remove(m.size()-1);
                    }
                    PictureSelector.create((Activity) mContext).themeStyle(R.style.picture_QQ_style).openExternalPreview(position, m);
                }
            }
        });

        holder.iv_img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteItem(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mImgs.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }


    /**
     * 删除图片
     * @param position
     */
    private void deleteItem(int position){
        mImgs.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position,mImgs.size()-position);
//        checkRecyclerView();
        checkSize();
    }

    public void clearAll(){
        if (mImgs != null){
            mImgs.clear();
        }
        notifyDataSetChanged();
        checkRecyclerView();
    }
    /**
     * 添加图片数据时通过adapter添加
     * @param img
     */
    public void addImgs(List<LocalMedia> img){
        if (mImgs.size()>0){
//            LocalMedia localMedia = mImgs.get(getItemCount() - 1);
//            if (TextUtils.equals("addImg",localMedia.getPath())){
//                mImgs.remove(getItemCount()-1);
//            }
            this.mImgs.addAll(getItemCount()-1,img);
        }else{
            this.mImgs.addAll(img);
        }
        notifyDataSetChanged();
        checkRecyclerView();
        checkSize();
    }

    private void checkSize() {
        if (getItemCount()>9){
            int position = getItemCount()-1;
            mImgs.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position,getItemCount()-1-position);
        }else if (getItemCount() < 9 && getItemCount() >0){
            LocalMedia localMedia = mImgs.get(getItemCount() - 1);
            if (!TextUtils.equals("addImg",localMedia.getPath())) {
//                SelectPhoto photo = new SelectPhoto();
//                photo.setImg(false);
                LocalMedia localMedia1 = new LocalMedia();
                localMedia1.setPath("addImg");
                mImgs.add(localMedia1);
//                notifyDataSetChanged();
                notifyItemInserted(getItemCount()-1);
            }else{
                if (getItemCount() == 1){
                    deleteItem(0);
                }
            }
        }else{
            checkRecyclerView();
        }
    }

    private void checkRecyclerView(){
        int itemCount = getItemCount();
        if (itemCount == 0){
            if (recyclerView !=null){
                recyclerView.setVisibility(View.GONE);
                if (mCallBack != null){
                    mCallBack.showImg(true);
                }
            }
        }else{
            if (recyclerView != null){
                recyclerView.setVisibility(View.VISIBLE);
                if (mCallBack != null){
                    mCallBack.showImg(false);
                }
            }
        }
    }

    static class Myholer extends RecyclerView.ViewHolder{

        private final RoundedImageView ri_img;
        private final ImageView iv_img_delete;
        private final ImageView iv_gif;
        public Myholer(View itemView) {
            super(itemView);
            ri_img = itemView.findViewById(R.id.ri_img);
            iv_img_delete = itemView.findViewById(R.id.iv_img_delete);
            iv_gif = itemView.findViewById(R.id.iv_gif);
        }
    }
}
